import { allDirection, getDictInfoByName } from '@/api/dict.js'
export const common = {
  namespaced: true,
  state: {
    colorObj: {
      red: '#ed4014',
      blue: '#5cadff',
      gray: '#808695',
      green: '#06b649'
    },
    directionData: [],
    dictData: {},
    requestLoading: {},
    requestPromise: {},
    fileMax: 10 * 1024, // 10 GB
    showDistributed: false, // 分布式业务控制开关，默认0为关闭，1为打开，只有针对分布式主靶场打开该开关才能使用
    sysConfig: {}
  },
  getters: {
    directionArr({ directionData }) {
      return directionData.filter((dict) => !dict.parentid)
    },
    typeArr({ directionData }) {
      return directionData.filter((dict) => dict.parentid)
    },
    treeDirectionData({ directionData }) {
      let treeDirectionData = {}
      directionData.forEach((dict) => {
        if (dict.parentid) {
          //二级
          if (!treeDirectionData[dict.parentid]) return
          treeDirectionData[dict.parentid].children.push({
            value: dict.dictid,
            label: dict.dictname,
            dictid: dict.dictid,
            dictname: dict.dictname,
            parentid: dict.parentid
          })
        } else if (!treeDirectionData[dict.dictid] && !dict.parentid) {
          //一级
          treeDirectionData[dict.dictid] = {
            value: dict.dictid,
            label: dict.dictname,
            dictid: dict.dictid,
            dictname: dict.dictname,
            children: []
          }
        }
      })
      return Object.values(treeDirectionData)
    }
  },
  mutations: {
    setDictData(state, data) {
      state.dictData[data.type] = data.dictData //dictData:{data,treeData} dictMixin 使用
    },
    setAllPoint(state, val) {
      state.directionData = val
    },
    setQuestStatus(state, val) {
      state.requestLoading[val.type] = val.status
    },
    setRequestPromise(state, val) {
      state.requestPromise[val.type] = val.promise
    },
    setDistributed(state, val) {
      state.showDistributed = val
    },
    setSysConfig(state, data) {
      state.sysConfig[data.name] = data.value
    }
  },
  actions: {
    // 课程方向
    getAllDirection({ commit, state }, type = 'newDirectionType') {
      if (state.requestLoading[type] === 'loading') {
        return state.requestPromise[type]
      }
      console.log('请求这里')
      let promise = new Promise((resolve, reject) => {
        allDirection({ dictTypeID: type })
          .then((data) => {
            if (data.code == 1) {
              let attribute = data.attribute.attribute[0]
              let directionData = attribute.dictEntryList
              directionData.forEach((dict) => {
                dict.key = dict.dictid
                dict.value = dict.dictname
              })
              if (type === 'newDirectionType') {
                commit('setAllPoint', directionData)
              }
              resolve(directionData)
            } else {
              reject(data.message)
            }
          })
          .catch((error) => {
            this.$Message.error('获取方向错误，请稍后再试!')
            reject(error)
          })
          .finally(() => {
            commit('setRequestPromise', { type, promise: null })
            commit('setQuestStatus', { type, status: 'end' })
          })
      })
      commit('setRequestPromise', { type, promise })
      commit('setQuestStatus', { type, status: 'loading' })
      return promise
    },
    // 是否显示
    getSysConfigShow({ commit }, name = '') {
      return new Promise((resolve) => {
        getDictInfoByName({ name })
          .then((res) => {
            if (res.code == 1) {
              // console.log(res);
              let value = res.attribute.data.value == '1' ? true : false
              commit('setSysConfig', { name, value })
              resolve(true)
            } else {
              commit('setSysConfig', { name, value: false })
              resolve(false)
            }
          })
          .catch((error) => {
            commit('setSysConfig', { name, value: false })
            resolve(false)
          })
      })
    },

    // 是否分布式隐藏
    getIsDistributed({ commit }) {
      getDictInfoByName({ name: 'isDistributed' })
        .then((res) => {
          if (res.code == 1) {
            // console.log(res);
            let value = res.attribute.data.value == '1' ? true : false
            commit('setDistributed', value)
          } else {
            commit('setDistributed', false)
          }
        })
        .catch((error) => {
          console.log('======')
        })
    }
  }
}
